package com.maimang.platform.activiti.controller;

import com.maimang.platform.activiti.dto.ProcessDefinitionDto;
import com.maimang.platform.activiti.dto.TaskEntityDto;
import com.maimang.platform.activiti.service.ActProcessService;
import com.maimang.platform.activiti.service.ActTaskService;
import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.utils.ShiroUtils;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname ActTaskController
 * @description 代办任务
 * @date 2019-04-22 20:24
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("activiti/task")
@Api(value = "任务管理", description = "待办任务相关操作接口")
public class ActTaskController extends BaseController {

	private String prefix = "activiti/task";

	@Autowired
	private ActTaskService actTaskService;

	@Autowired
	private ActProcessService actProcessService;

	/**
	 * 进入待办任务页
	 *
	 * @return
	 */
	@RequiresPermissions("activiti:task:view")
	@GetMapping
	public String tasks() {
		return prefix + "/tasks";
	}

	/**
	 * 根据任务实体对象查询任务列表
	 *
	 * @param taskEntityDto 任务实体对象
	 * @return
	 */
	@ApiOperation(value = "任务列表", notes = "根据任务实体对象查询任务列表")
	@RequiresPermissions("activiti:task:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo getTaskList(TaskEntityDto taskEntityDto) {

		if (!"admin".equalsIgnoreCase(ShiroUtils.getLoginName())) {
			taskEntityDto.setAssignee(String.valueOf(ShiroUtils.getUserId()));
		}
		return actTaskService.selectTaskEntityList(taskEntityDto);
	}


	/**
	 * 进入任务申请列表
	 *
	 * @return
	 */
	@RequiresPermissions("activiti:task:apply")
	@GetMapping("apply")
	public String applyTask() {
		return prefix + "/apply";
	}


	/**
	 * 查询可申请的流程
	 *
	 * @param processDefinitionDto 流程实体数据对象
	 * @return
	 */
	@RequiresPermissions("activiti:task:applyList")
	@PostMapping("/applyList")
	@ResponseBody
	public TableDataInfo getApplyList(ProcessDefinitionDto processDefinitionDto) {
		return actProcessService.selectProcessDefinitionList(processDefinitionDto);
	}

	/**
	 * 跳转到新增
	 *
	 * @return
	 */
	@RequiresPermissions("activiti:task:add")
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 编辑
	 *
	 * @param taskId
	 * @param map
	 * @return
	 */
	@RequiresPermissions("activiti:task:edit")
	@PostMapping("/edit/{taskId}")
	@ResponseBody
	public String edit(@PathVariable("taskId") String taskId, ModelMap map) {
		TaskEntityDto taskEntityDto = actTaskService.selectOneTask(taskId);
		map.put("task", taskEntityDto);
		return prefix + "/edit";
	}

	/**
	 * 更新任务
	 *
	 * @param taskEntityDto
	 * @param modelMap
	 * @return
	 */
	@Log(title = "更新任务", businessType = BusinessType.OTHER)
	@RequiresPermissions("activiti:task:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Result updateTask(TaskEntityDto taskEntityDto, ModelMap modelMap) {
		actTaskService.completeTask(taskEntityDto.getId(), modelMap);
		return toResult(1);
	}


	/**
	 * 待办任务
	 *
	 * @param taskEntityDto
	 * @param modelMap
	 * @return
	 */
	@Log(title = "待办任务", businessType = BusinessType.OTHER)
	@RequiresPermissions("activiti:task:view")
	@PostMapping("/todo")
	@ResponseBody
	public TableDataInfo getTodoTaskList(TaskEntityDto taskEntityDto, ModelMap modelMap) {
		taskEntityDto.setAssignee(ShiroUtils.getLoginName());
		return actTaskService.selectTodoTask(taskEntityDto);
	}


	/**
	 * 受邀任务
	 *
	 * @param taskEntityDto 任务实体
	 * @param modelMap
	 * @return
	 */
	@Log(title = "受邀任务", businessType = BusinessType.OTHER)
	@RequiresPermissions("activiti:task:view")
	@PostMapping("/invited")
	@ResponseBody
	public TableDataInfo getInvitedTaskList(TaskEntityDto taskEntityDto, ModelMap modelMap) {
		taskEntityDto.setInvitedUser(ShiroUtils.getLoginName());
		return actTaskService.selectInvitedTask(taskEntityDto);
	}


	/**
	 * 归档任务
	 *
	 * @param taskEntityDto 任务实体
	 * @param modelMap
	 * @return
	 */
	@Log(title = "归档任务", businessType = BusinessType.OTHER)
	@RequiresPermissions("activiti:task:view")
	@PostMapping("/archived")
	@ResponseBody
	public TableDataInfo getArchivedTaskList(TaskEntityDto taskEntityDto, ModelMap modelMap) {
		taskEntityDto.setOwner(ShiroUtils.getLoginName());
		return actTaskService.selectArchivedTask(taskEntityDto);
	}

	/**
	 * 查询完成的任务
	 *
	 * @param taskEntityDto 任务实体
	 * @param modelMap
	 * @return
	 */
	@Log(title = "归档任务", businessType = BusinessType.OTHER)
	@RequiresPermissions("activiti:task:view")
	@PostMapping("/finished")
	@ResponseBody
	public TableDataInfo getFinishedTaskList(TaskEntityDto taskEntityDto, ModelMap modelMap) {
		taskEntityDto.setOwner(ShiroUtils.getLoginName());
		return actTaskService.selectFinishedTask(taskEntityDto);
	}

	/**
	 * 进入流程表单
	 *
	 * @param processId
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/form/{processId}")
	public String startForm(@PathVariable("processId") String processId, HttpServletResponse response) throws IOException {
		String formKey = actTaskService.getFormKey(processId, null);
		return redirect(formKey);
	}

	/**
	 * 获取流程XML上的表单KEY
	 *
	 * @param processId 流程id
	 * @param taskId    任务节点id
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/form/{processId}/{taskId}")
	public String startForm(@PathVariable("processId") String processId, @PathVariable("taskId") String taskId) throws IOException {
		String formKey = actTaskService.getFormKey(processId, taskId);
		return redirect(formKey);
	}

	/**
	 * 读取带跟踪的图片
	 *
	 * @param processId   流程id
	 * @param executionId 执行id
	 * @param response
	 * @throws IOException
	 */
	@GetMapping("/trace/photo/{processId}/{executionId}")
	public void traceTaskPhoto(@PathVariable("processId") String processId, @PathVariable("executionId") String executionId, HttpServletResponse response) throws IOException {
		InputStream inputStream = actTaskService.traceTaskPhoto(processId, executionId);
		// 输出资源内容到相应对象
		byte[] b = new byte[1024];
		int len;
		while ((len = inputStream.read(b, 0, 1024)) != -1) {
			response.getOutputStream().write(b, 0, len);
		}
	}

}
